package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Clxx;
import com.hnkywl.sts.util.Constant;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2016/6/22.
 */
@Repository
public class ClxxDao extends SimpleHibernateTemplate<Clxx, Long> {

    public Map<String, Object> findClxxList(String clh, String xshth, Date ccrqstart, Date ccrqend, String ccch, String zzyhmc, int pageNo, int pageSize) {
        Map<String, Object> param = new HashMap<String, Object>();
        List<String> condition = new ArrayList<String>();
        Map<String, Object> map = new HashMap<String, Object>();

        //===============搜索查询条件======================
        if (clh != null && !clh.isEmpty()) {
            condition.add(" clxx.clh like :clh ");
            param.put("clh", "%" + clh.trim() + "%");
        }
        if (xshth != null && !xshth.isEmpty()) {
            condition.add(" clxx.xshth like :xshth ");
            param.put("xshth", "%" + xshth.trim() + "%");
        }

        if (ccrqstart != null) {
            condition.add(" clxx.ccrq >= :ccrqstart");
            param.put("ccrqstart", Constant.sDaysAfterNowDate(ccrqstart));
        }

        if (ccrqend != null) {
            condition.add(" clxx.ccrq < :ccrqend");
            param.put("ccrqend", Constant.jDaysAfterNowDate(ccrqend));
        }

        if (ccch != null && !ccch.isEmpty()) {
            condition.add(" clxx.ccch like :ccch ");
            param.put("ccch", "%" + ccch.trim() + "%");
        }

        if (zzyhmc != null && !zzyhmc.isEmpty()) {
            condition.add(" clxx.zzyhmc like :zzyhmc ");
            param.put("zzyhmc", "%" + zzyhmc.trim() + "%");
        }


        //==============记录数量===============
        String sqlCountBase = "select count(1) from ";

        //==============查询结果===============
        String sqlBase = "  select clxx.id as \"id\",\n" +
                "        clxx.jd as \"jd\",\n" +
                "        clxx.ccrq as \"ccrq\",\n" +
                "        clxx.ccch as \"ccch\",\n" +
                "        clxx.xshth as \"xshth\",\n" +
                "        clxx.cljz as \"cljz\",\n" +
                "        clxx.clmz as \"clmz\",\n" +
                "        clxx.cljs as \"cljs\",\n" +
                "        clxx.ckmdh as \"ckmdh\",\n" +
                "        clxx.xsgsmc as \"xsgsmc\",\n" +
                "        clxx.dyshyhmc as \"dyshyhmc\",\n" +
                "        clxx.deshyhmc as \"deshyhmc\",\n" +
                "        clxx.dzmc as \"dzmc\",\n" +
                "        clxx.jsyhmc as \"jsyhmc\",\n" +
                "        clxx.zzyhmc as \"zzyhmc\",\n" +
                "        clxx.gg as \"gg\",\n" +
                "        clxx.ph as \"ph\",\n" +
                "        clxx.pzmc as \"pzmc\",\n" +
                "        clxx.ckzt as \"ckzt\",\n" +
                "        clxx.cjsj as \"cjsj\",\n" +
                "        clxx.clh as \"clh\",\n" +
                "        clxx.zt as \"zt\",\n" +
                "        clxx.bz as \"bz\"\n" +
                "   from t_biz_clxx clxx";

        String whereSql = "";
        String orderSql = " order by clxx.cjsj desc";
        if (!condition.isEmpty())
            whereSql += " where " + StringUtils.join(condition, " and ");
        String countSql = sqlCountBase + " ( " + sqlBase + whereSql + orderSql + " ) ";
        String resultSql = sqlBase + whereSql + orderSql;
        BigDecimal count = (BigDecimal) createSQLQuery(countSql).setProperties(param).uniqueResult();
        map.put("total", count);
        List<Map> list = new ArrayList<Map>();
        if (count.intValue() != 0) {
            list = createSQLQuery(resultSql)
                    .setProperties(param)
                    .setFirstResult((pageNo - 1) * pageSize)
                    .setMaxResults(pageSize)
                    .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
                    .list();
        }
        map.put("rows", list);
        return map;
    }

    /**
     * 根据材料号查出材料
     *
     * @param clh
     * @return
     */
    public Clxx findClxxByClh(String clh) {
        String hql = "from Clxx where clh = ?";
        return (Clxx) findUnique(hql, clh);
    }

    public Boolean checkClh(String clh) {
        String sql = "select count(1) from t_biz_clxx where clh = ?";
        BigDecimal count = (BigDecimal) createSQLQuery(sql).setParameter(0, clh.trim()).uniqueResult();
        if (count.doubleValue() > 0d) {
            return false;
        } else {
            return true;
        }
    }
}
